import threading
from datetime import datetime, timedelta

import requests

lis = [i for i in range(0, 100000) if i % 500 == 0]


def insert(start):
    fmt_s = '%Y%m%d%H%M%S'
    for j in range(start + 1, start + 501):
        device_id = str(10000000 + j)
        tm = datetime(2022, 10, 1, 0, 0)
        with requests.Session() as s:
            for i in range(2880):
                tm_res = str(tm.strftime(fmt_s))[::-1]
                row_key = tm_res + device_id + 'mi15'
                data = {
                    "hbase_rowKey": row_key,
                    "cf#frequency": "mi15",
                    "cf#data_time": tm_res,
                    "cf#device_id": device_id
                }
                # re.auto_request(url=f'192.168.11.105:9200/xncs_table/_create/{row_key}', body=data,
                #                 timeout=30)
                # with requests.Session() as s:
                try:
                    s.post(url=f'http://221.226.153.90:19200/xncs_hbase/_create/{row_key}', json=data, timeout=30)
                    # print(f'设备{device_id}请求成功')
                except Exception:
                    print('出错了---------------------------------------------------')
                tm += timedelta(minutes=15)
        print(j)


if __name__ == '__main__':
    # 多进程运行：
    # time_start = datetime.now()
    # process_list = []
    # for i in lis:  # 开启5个子进程执行fun1函数
    #     p = Process(target=insert, args=(i,))  # 实例化进程对象
    #     p.start()
    #     process_list.append(p)
    # for i in process_list:
    #     i.join()
    # time_end = datetime.now()
    # res = time_end - time_start
    # print(res)

    # 多线程运行：
    time_start = datetime.now()
    threading_list = []
    for i in lis:
        t = threading.Thread(target=insert, args=(i,))
        t.start()
        threading_list.append(t)
    for i in threading_list:
        i.join()
    time_end = datetime.now()
    res = time_end - time_start
    print(res)
